import 'dart:convert';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:login/config/SystemConfig.dart';
// import '../util/Storage.dart';

class RegisterPage2 extends StatefulWidget {
  final String tel;
  RegisterPage2({Key key,this.tel}) : super(key: key);


  _RegisterPage2State createState() => _RegisterPage2State();
}

class _RegisterPage2State extends State<RegisterPage2> {
  
  TextEditingController _nickname = new TextEditingController();
  TextEditingController _pwd = new TextEditingController();
  TextEditingController _pwdSecond = new TextEditingController();

  void _register() async {
    String api = SystemConfig.BASE_URL + '/appUser/registerUser';
    Response response = await Dio().post(api, queryParameters: {
      'mobile': '${this.widget.tel}',
      'pwd': '${this._pwd.text}',
      'nickname': '${this._nickname.text}'
    });
    Map result = json.decode(response.toString());
    if (this._pwd.text == this._pwdSecond.text) {
      if (_nickname.text != '') {
        if (this._pwd.text != '' && this._pwdSecond.text != '') {
          if (response.statusCode == 200 &&
              result != null &&
              result['success']) {
            this._nickname.text = '';
            this._pwd.text = '';
            this._pwdSecond.text = '';
            print(result.toString());
            Fluttertoast.showToast(msg: result['message']);
            Navigator.pushReplacementNamed(context, '/login');
          }
        } else {
          Fluttertoast.showToast(
              msg: '密码不能为空，请重新输入！',
              gravity: ToastGravity.BOTTOM,
              textColor: Colors.red,
              backgroundColor: Colors.white);
        }
      } else {
        Fluttertoast.showToast(
            msg: '请您输入昵称！',
            gravity: ToastGravity.BOTTOM,
            textColor: Colors.red,
            backgroundColor: Colors.white);
      }
    } else {
      Fluttertoast.showToast(
          msg: '注册失败，请检查前后密码是否一致！',
          gravity: ToastGravity.BOTTOM,
          textColor: Colors.red,
          backgroundColor: Colors.white);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('注册'),
        ),
        body: Container(
          padding: EdgeInsets.all(20.0),
          child: ListView(
            children: <Widget>[
              //显示手机号码
              Container(
                  margin: EdgeInsets.only(top: 20.0, bottom: 20.0),
                  child: Text(
                    '您的手机号码是：${this.widget.tel}',
                    style: TextStyle(color: Colors.black, fontSize: 20.0),
                  )),
              //请输入您的昵称
              Container(
                margin: EdgeInsets.only(bottom: 20.0),
                child: TextField(
                  keyboardType: TextInputType.number,
                  cursorColor: Colors.orangeAccent, //光标颜色
                  //文本框的描述提示，默认的填充文本（hintText）
                  decoration: InputDecoration(hintText: '请输入您的昵称'),
                  onChanged: (value) {
                    //文本改变事件，每次输入的内容就会赋值给文本框
                    this._nickname.text = value;
                    this._nickname.selection = TextSelection.fromPosition(
                        TextPosition(offset: this._nickname.text.length));
                  },
                ),
              ),
              //请输入您的密码
              Container(
                margin: EdgeInsets.only(bottom: 20.0),
                child: TextField(
                  keyboardType: TextInputType.number,
                  cursorColor: Colors.orangeAccent,
                  decoration: InputDecoration(hintText: '请输入您的密码'),
                  onChanged: (value) {
                    //文本改变事件，每次输入的内容就会赋值给文本框
                    this._pwd.text = value;
                    this._pwd.selection = TextSelection.fromPosition(
                        TextPosition(offset: this._pwd.text.length));
                  },
                ),
              ),
              //请再次输入您的密码
              Container(
                margin: EdgeInsets.only(bottom: 20.0),
                child: TextField(
                  keyboardType: TextInputType.number,
                  cursorColor: Colors.orangeAccent,
                  decoration: InputDecoration(hintText: '请再次输入您的密码'),
                  onChanged: (value) {
                    //文本改变事件，每次输入的内容就会赋值给文本框
                    this._pwdSecond.text = value;
                    this._pwdSecond.selection = TextSelection.fromPosition(
                        TextPosition(offset: this._pwdSecond.text.length));
                  },
                ),
              ),

              //下一步按钮
              RaisedButton(
                child: Text(
                  '提交',
                  style: TextStyle(color: Colors.white, fontSize: 14.0),
                ),
                color: Colors.blue[300],

                //事件为null禁用

                shape: new RoundedRectangleBorder(
                    //设置为圆角
                    borderRadius: BorderRadius.circular(15),
                    //无边框
                    side: BorderSide.none),
                onPressed: () {
                  _register();
                  // Navigator.pushNamed(context, '/login');
                },
              )
            ],
          ),
        ));
  }
}
